# -*- coding: utf-8 -*-
# FileName:     custom_module.py
# time:         23/2/20 020 下午 12:10
# Author:       Zhou Hang
# Description:  I don't want to write
from PyQt5.QtWidgets import QTableWidget, QHeaderView, QTableWidgetItem


class RealTimeInfoTable(QTableWidget):
    def __init__(self, parent):
        super(RealTimeInfoTable, self).__init__(parent)
        self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)

    @staticmethod
    def extract_detect_info(record: str):
        """
        Position: (0, 378), (144, 360), Obj and Confidence: suitcase 0.27
        """
        positions, obj_confidence = record.split(':')[1:]
        obj, confidence = obj_confidence.strip().rsplit(' ', 1)
        positions = positions.strip().split(', ')
        p1 = ','.join(positions[0:2])
        p2 = ','.join(positions[2:4])
        return p1, p2, obj, confidence

    def update_info(self, info_show: str):
        """
        info_show形如
        Position: (0, 378), (144, 360), Obj and Confidence: suitcase 0.27
        Position: (88, 175), (216, 157), Obj and Confidence: person 0.51
        """
        self.clearContents()
        info_show = info_show.split('\n')
        info_show_len = len(info_show) - 1
        self.setRowCount(info_show_len)
        for i, record in enumerate(info_show):
            if record == '':
                break
            position1, position2, obj, confidence = self.extract_detect_info(record)
            item = QTableWidgetItem(position1)
            self.setItem(i, 0, item)
            item = QTableWidgetItem(position2)
            self.setItem(i, 1, item)
            item = QTableWidgetItem(obj)
            self.setItem(i, 2, item)
            item = QTableWidgetItem(confidence)
            self.setItem(i, 3, item)


def main():
    pass


if __name__ == "__main__":
    main()
